package com.gla.giliair.web.customer;

import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/24 10:27
 */
@Api(tags = "航班模块")
@RestController
@RequestMapping("/api")
public class FlightController extends BaseController {

    @ApiOperation(value = "查询航班列表", notes = "根据出发城市、到达城市、出发日期（当日），查询航班列表，有两种排序方式", produces = "application/json")
    @PostMapping("/searchFlight")
    @ResponseBody
    public Map<String, Object> searchFlight(@RequestParam("leavePoint") String leavePoint,
                                            @RequestParam("arrivePoint") String arrivePoint,
                                            //举例：2021-08-12
                                            @RequestParam("beginDay") String beginDay,
                                            //排序关键词：time(出发日期)、price(最低折扣票价)
                                            @RequestParam(value = "sort", required = false) String sort,
                                            //正反序只对关键词生效
                                            @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize) {
        if (!StringUtils.hasText(leavePoint)) {
            return MapGenerator.getRes(500, "请输入出发城市！");
        }
        if (!StringUtils.hasText(arrivePoint)) {
            return MapGenerator.getRes(501, "请输入到达城市！");
        }
        if (!StringUtils.hasText(beginDay)) {
            return MapGenerator.getRes(502, "请输入出发日期！");
        }
        Date beginDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            beginDate = sdf.parse(beginDay);
        } catch (ParseException e) {
            e.printStackTrace();
            return MapGenerator.getRes(503, "出发日期格式不正确！");
        }
        //过滤并统一化sort参数
        sort = sort == null ? "" : sort;
        switch (sort) {
            case "time":
                sort = "leaveTime";
                break;
            case "price":
                sort = "leastPrice";
                break;
            default:
                sort = "";
        }
        return flightService.findFlightsInADayByLeaveAndArriveAndTime(leavePoint, arrivePoint, beginDate, sort, isASC, page, pageSize);
    }

    @ApiOperation(value = "获取低价航班列表", notes = "根据出发城市、时间段，获取低价航班列表(按最低折扣票价，从小到大排序)", produces = "application/json")
    @PostMapping("/findSpecialFlight")
    @ResponseBody
    public Map<String, Object> findSpecialFlight(@RequestParam("leavePoint") String leavePoint,
                                                 //举例：2021-08-01
                                                 @RequestParam("beginIn") String beginIn,
                                                 //举例：2021-09-01
                                                 @RequestParam("endIn") String endIn,
                                                 @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                 @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize) {
        if (!StringUtils.hasText(leavePoint)) {
            return MapGenerator.getRes(500, "请输入出发城市！");
        }
        if (!StringUtils.hasText(beginIn)) {
            return MapGenerator.getRes(502, "请输入起始日期！");
        }
        if (!StringUtils.hasText(endIn)) {
            return MapGenerator.getRes(503, "请输入结束日期！");
        }
        Date beginDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            beginDate = sdf.parse(beginIn);
            endDate = sdf.parse(endIn);
        } catch (ParseException e) {
            e.printStackTrace();
            return MapGenerator.getRes(504, "日期格式不正确！");
        }
        return flightService.findSpecialFlightsByLeaveAndTime(leavePoint, beginDate, endDate, "leastPrice", true, page, pageSize);
    }
}
